package com.proj.hot.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.proj.hot.GlobalParams;
import com.proj.hot.R;
import com.proj.hot.utils.ImageLoaderConfigFactory;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * 观看记录
 * 
 */
public class RecordGridAdapter extends BaseAdapter
{
	private List<JSONObject> arrayList;
	private Context context;

	public RecordGridAdapter(Context context, List<JSONObject> arrayList)
	{
		if (arrayList == null)
		{
			this.arrayList = new ArrayList<JSONObject>();
		}
		else
		{
			this.arrayList = arrayList;
		}
		this.context = context;
	}
	
	@Override
	public int getCount()
	{
		return arrayList.size();
	}
	
	@Override
	public Object getItem(int position)
	{
		return arrayList.get(position);
	}
	
	public void setList(List<JSONObject> list)
	{
		if (list == null)
		{
			this.arrayList = new ArrayList<JSONObject>();
		}
		else
		{
			this.arrayList = list;
		}
	}
	
	@Override
	public long getItemId(int position)
	{
		return position;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		ViewHolder holder = null;
		if (convertView == null)
		{
			holder = new ViewHolder();
			convertView = View.inflate(context, R.layout.record_grid_item, null);
			holder.poster = (ImageView) convertView.findViewById(R.id.record_poster);
			holder.name = (TextView) convertView.findViewById(R.id.record_name);
			holder.time = (TextView) convertView.findViewById(R.id.record_time);
			convertView.setTag(holder);
		}
		else
		{
			holder = (ViewHolder) convertView.getTag();
		}
		try {
			holder.poster.setImageResource(R.drawable.morentu);
			ImageLoader.getInstance().displayImage(arrayList.get(position).getString("logo"),
					holder.poster, ImageLoaderConfigFactory.getOptionsWithNopic());
			if (GlobalParams.screenHeight == 1080){
//				LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) holder.name
//						.getLayoutParams();
//				lp.setMargins(0, 0, 0, 0);
//				lp.height = 40;
//				holder.name.setLayoutParams(lp);
				holder.name.setTextSize(27);
			}
			holder.name.setText(arrayList.get(position).getString("name"));
			//holder.time.setText(arrayList.get(position).getTime());
		}catch (Exception e){
			e.printStackTrace();
		}
		return convertView;
	}
	
	class ViewHolder
	{
		private ImageView poster;
		private TextView name;
		private TextView time;

	}
}
